//
//  JSLProductCell.m
//  JingleSpeedLoan
//
//  Created by 宋质彬 on 2018/6/22.
//  Copyright © 2018年 宋质彬. All rights reserved.
//

#import "JSLProductCell.h"

@interface JSLProductCell ()
@property (nonatomic , weak) UIView *backView;
@property (nonatomic , weak) UIImageView *logoImage;
@property (nonatomic , weak) UILabel *productName;
@property (nonatomic , weak) UILabel *productScr;
@property (nonatomic , weak) UILabel *textLab1;
@property (nonatomic , weak) UILabel *textLab2;
@property (nonatomic , weak) UILabel *textLab3;

@property (nonatomic , strong) NSMutableArray *textArray0;
@property (nonatomic , strong) NSMutableArray *textArray1;

@end

@implementation JSLProductCell

#pragma mark - init
- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.backgroundColor = BACKGROUND_COLOR;
        self.textArray0 = [[NSMutableArray alloc] init];
        self.textArray1 = [[NSMutableArray alloc] init];
        
        [self setUpAllChildView];
        
    }
    return self;
}

- (void)configProductCellContent:(NSDictionary *)info {
 

    [self.logoImage sd_setImageWithURL:[NSURL URLWithString:info[@"productLogo"]] placeholderImage:nil];
    
    self.productName.text = info[@"productName"];

    self.productScr.text = info[@"loanDesc"];
    
    NSArray *labArray = [info[@"productTag"] componentsSeparatedByString:@","];
    
    if (labArray.count == 2) {
        self.textLab1.text = [labArray lastObject];
        self.textLab2.text = [labArray firstObject];
     
    } else if (labArray.count == 1) {
        self.textLab1.text = [labArray firstObject];
    }
    
    NSString *rate = @"";
    if ([JSLKitFactory isNotNull:info[@"dayRate"]]) {
        rate = [NSString stringWithFormat:@"%@%@",info[@"dayRate"],@"%"];
    }else {
        rate = [NSString stringWithFormat:@"0.00%@",@"%"];
    }
    
    NSString *time = @"";
    if ([JSLKitFactory isNotNull:info[@"dayRate"]]) {
        time = info[@"lendTime"];
    }else {
        time = @"~";
    }
    
    NSString *highAmount = @"";
    if ([JSLKitFactory isNotNull:info[@"highAmount"]]) {
        highAmount = info[@"highAmount"];
    }else {
        highAmount = @"0.00";
    }
    
    
    NSString *lowAmount = @"";
    if ([JSLKitFactory isNotNull:info[@"lowAmount"]]) {
        lowAmount = info[@"lowAmount"];
    }else {
        lowAmount = @"0.00";
    }
    
  
    
    NSArray *valueArray = @[rate,[NSString stringWithFormat:@"%@~%@",lowAmount,highAmount],time];
    for (NSInteger i = 0; i <self.textArray1.count; i ++) {
        UILabel *lab = self.textArray1[i];
        lab.text = valueArray[i];
    }
    
}

#pragma mark - 配置视图
- (void)layoutSubviews{
    [super layoutSubviews];
    [self configFrame];
}

- (void)configFrame{
    
    self.backView.frame = CGRectMake(10*AutoSizeScaleXX, 0, CGRectGetWidth(self.frame)-10*AutoSizeScaleXX*2, CGRectGetHeight(self.frame)-10*AutoSizeScaleYY);
    
    
    
    self.logoImage.frame = CGRectMake(10*AutoSizeScaleXX, 15*AutoSizeScaleYY, 32*AutoSizeScaleXX, 32*AutoSizeScaleXX);
    self.logoImage.layer.mask = [JSLKitFactory roundedRect:self.logoImage.bounds cornerRadius:2.0];
    
    CGSize titleSize = [self.productName.text sizeWithAttributes:@{NSFontAttributeName : self.productName.font}];
    self.productName.frame = CGRectMake(CGRectGetMaxX(self.logoImage.frame)+7*AutoSizeScaleXX, 10*AutoSizeScaleYY, titleSize.width, titleSize.height);
    
    CGSize subSize = [self.productScr.text sizeWithAttributes:@{NSFontAttributeName : self.productScr.font}];
    self.productScr.frame = CGRectMake(CGRectGetMinX(self.productName.frame), CGRectGetMaxY(self.productName.frame) +5*AutoSizeScaleYY, subSize.width, subSize.height);
    
    CGSize textSize = [self.textLab1.text sizeWithAttributes:@{NSFontAttributeName : self.textLab1.font}];
    self.textLab1.frame = CGRectMake(CGRectGetWidth(self.backView.frame)-10*AutoSizeScaleXX -textSize.width -18*AutoSizeScaleXX, 15*AutoSizeScaleYY, textSize.width +18*AutoSizeScaleXX, textSize.height +4*AutoSizeScaleYY);
    
    CGSize textSize2 = [self.textLab2.text sizeWithAttributes:@{NSFontAttributeName : self.textLab2.font}];
    self.textLab2.frame = CGRectMake(CGRectGetMinX(self.textLab1.frame)-10*AutoSizeScaleXX -textSize2.width-18*AutoSizeScaleXX, 15*AutoSizeScaleYY, textSize2.width +18*AutoSizeScaleXX, textSize2.height +4*AutoSizeScaleYY);
    
     CGFloat width = (CGRectGetWidth(self.backView.frame))/3.0;
    for (NSInteger i = 0; i < self.textArray0.count; i ++) {
        UILabel *textLab = self.textArray0[i];
        CGSize size1 = [textLab.text sizeWithAttributes:@{NSFontAttributeName :textLab.font }];
        textLab.frame = CGRectMake(width *i, CGRectGetMaxY(self.logoImage.frame)+10*AutoSizeScaleYY, width, size1.height);
        
        UILabel *textLablow = self.textArray1[i];
        textLablow.frame = CGRectMake(width *i, CGRectGetMaxY(textLab.frame)+3*AutoSizeScaleYY, width, size1.height+2*AutoSizeScaleYY);
    }
   
    
    if (self.loanCellType == LoanCelllTypeNormal) {
        
    }
    
    
    
}
- (void)setUpAllChildView{
   
    self.backView = [JSLKitFactory jsl_viewWithFrame:CGRectZero backgroundColor:nil superView:self.contentView];
    self.backView.backgroundColor = [UIColor whiteColor];
    
    self.productName = [JSLKitFactory jsl_labelWithFrame:CGRectZero text:@"账单" font:MEDIUM_FONT(14) textColor:[@"#333333" hexStringToColor] textAlignment:NSTextAlignmentLeft SuperView:self.backView];
    
    self.productScr = [JSLKitFactory jsl_labelWithFrame:CGRectZero text:@"好用的账单" font:REGULAR_FONT(10) textColor:[@"#666666" hexStringToColor] textAlignment:NSTextAlignmentLeft SuperView:self.backView];
    self.logoImage = [JSLKitFactory jsl_imageViewWithFrame:CGRectZero imageName:@"" superView:self.backView];
    
    self.textLab1 = [JSLKitFactory jsl_labelWithFrame:CGRectZero text:@"" font:LIGHT_FONT(10) textColor:[@"#64B3FF" hexStringToColor] textAlignment:NSTextAlignmentCenter SuperView:self.backView];
    
    self.textLab1.backgroundColor = [@"#C9E5FF" hexStringToColor];
    
    self.textLab2 = [JSLKitFactory jsl_labelWithFrame:CGRectZero text:@"" font:LIGHT_FONT(10) textColor:[@"#FC5B35" hexStringToColor] textAlignment:NSTextAlignmentCenter SuperView:self.backView];
    self.textLab2.backgroundColor = [@"#FFE9E9" hexStringToColor];
    
    self.textLab3 = [JSLKitFactory jsl_labelWithFrame:CGRectZero text:nil font:LIGHT_FONT(10) textColor:nil textAlignment:NSTextAlignmentCenter SuperView:self.backView];
    
    NSArray *textArray = @[@"参考日利率",@"额度范围(元)",@"预计放款时间"];
    NSArray *valueArray = @[@"0.870%",@"0.870%",@"0.870%"];
   

    for (NSInteger i = 0; i < textArray.count; i ++) {
        UILabel *textLab = [JSLKitFactory jsl_labelWithFrame:CGRectZero text:textArray[i] font:LIGHT_FONT(10) textColor:[@"#999999" hexStringToColor] textAlignment:NSTextAlignmentCenter SuperView:self.backView];
        [self.textArray0 addObject:textLab];

        
        UILabel *textLablow = [JSLKitFactory jsl_labelWithFrame:CGRectZero text:valueArray[i] font:LIGHT_FONT(12) textColor:[@"#FC5B35" hexStringToColor] textAlignment:NSTextAlignmentCenter SuperView:self.backView];
        [self.textArray1 addObject:textLablow];

        
    }
}

#pragma mark - 创建cell
+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    static NSString * ID = @"JSLProductCell";
    id cell = [tableView dequeueReusableCellWithIdentifier:ID ];
    
    if (cell == nil) {
        cell = [[self alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:ID];
    }
    
    return cell;
}
- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}


@end
